#!groovy
pipeline {
    agent any

    options {
        timeout(time: 100, unit: 'MINUTES')
    }
    environment {
        PROJECT_NAME = "AvatarScrmCompany"
        REPOSITORY = "git@62.234.219.180:AUDIT-PLAT/audit-plat-manage.git"
        BRANCH = "dev"
        LANG = "Java"
        BASEPATH = "/var/jenkins_home"
        WORKSPACE = "${BASEPATH}/workspace/PIPELINE2/Pipeline-${PROJECT_NAME}"
        CIPATH = "${BASEPATH}/CI/${PROJECT_NAME}"
        TOOLPATH="${BASEPATH}/workspace/tools/RPTEST"
    }
    stages {
        stage('克隆') {
            steps {
                echo "清楚旧数据"
                sh 'cd ${WORKSPACE};rm -rf *'
                echo "开始从git仓:${REPOSITORY}拉取代码"
                echo  "拉取分支:${BRANCH}"
                git branch: "${BRANCH}", url: "${REPOSITORY}"
                echo "拉取结束"
            }
        }

        stage('编译打包'){
            steps {
                withMaven(maven: 'maven-3.3.9') {
                    ws("${WORKSPACE}/hillinsight-scrm-company") {
                        sh "mvn clean package -Dproject.build.sourceEncoding=UTF-8 -Ptest -B -e -U -Dmaven.test.skip=true"
                    }
                }
            }
        }

        stage('文件存档'){
            steps{
                sh '${TOOLPATH}/filing.sh ${CIPATH} ${WORKSPACE} ${PROJECT_NAME} ${LANG}'
            }
        }
        stage('镜像存档'){
            steps{
                sh "${TOOLPATH}/image.sh ${CIPATH} ${PROJECT_NAME} ${LANG} 'CMD java -Xms512m -Xmx1024m -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:/data/log/project/gc.log -jar AvatarScrmCompany.jar'"
            }
        }
        stage('更新QA环境'){
            steps{
                sh '${TOOLPATH}/deployQa.sh avatarscrmcompany avatarscrmcompany ${CIPATH}'
            }
        }
        stage('获取提交信息') {
            steps {
                script{
                    List<String> changes = getChangedFilesList()
                    println ("Changed file list: " + changes)
                    env.CHANGES = changes;

                    String gitCommitId = getGitcommitID()
                    println("GIT CommitID: " + gitCommitID)
                    env.GITCOMMITID = gitCommitId;

                    String authorName = getAuthorName()
                    println("GIT CommitAuthorName: " + authorName)
                    env.AUTHORNAME = authorName;

                    String gitCommitMessage = getCommitMessage()
                    println("GIT CommitMessage: " + gitCommitMessage)
                    env.MESSAGE = gitCommitMessage;

                }
            }
        }
    }
    post {
        success {
            sh '''
            curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=33692558-a80b-4139-9b1d-2cbc55ead5da' \
            -H 'Content-Type: application/json' \
            -d '
            {
                "msgtype": "markdown",
                "markdown": {
                    "content": "#测试环境构建成功:'${PROJECT_NAME}'服务!\n
                        >### 构建地址 '${REPOSITORY}'
                        >### 构建分支 '${BRANCH}'
                        >### 提交文件 '${CHANGES}'
                        >### 提交信息 '${MESSAGE}'
                        >### 提交作者 '${AUTHORNAME}'"
                }
            }'
            '''
        }
        failure {
            sh '''
            curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=33692558-a80b-4139-9b1d-2cbc55ead5da' \
             -H 'Content-Type: application/json' \
             -d '
            {
                "msgtype": "markdown",
                "markdown": {
                "content": "#测试环境构建失败:'${PROJECT_NAME}'服务!\n
                    >### 构建地址 '${REPOSITORY}'
                    >### 构建分支 '${BRANCH}'
                    >### 提交文件 '${CHANGES}'
                    >### 提交信息 '${MESSAGE}'
                    >### 提交作者 '${AUTHORNAME}'"
                }
            }'
            '''
            echo 'failure'
        }
    }
}
@NonCPS
List<String> getChangedFilesList(){
    def changedFiles = []
    for ( changeLogSet in currentBuild.changeSets){
        for (entry in changeLogSet.getItems()){
            changedFiles.addAll(entry.affectedPaths)
        }
    }
    return changedFiles
}

@NonCPS
String getGitcommitID(){
    gitCommitID = " "
    for ( changeLogSet in currentBuild.changeSets){
        for (entry in changeLogSet.getItems()){
            gitCommitID = entry.commitId
        }
    }
    return gitCommitID
}

@NonCPS
String getAuthorName(){
    gitAuthorName = " "
    for ( changeLogSet in currentBuild.changeSets){
        for (entry in changeLogSet.getItems()){
            gitAuthorName = entry.authorName
        }
    }
    return gitAuthorName
}

@NonCPS
String getCommitMessage(){
    gitCommitMessage = " "
    for ( changeLogSet in currentBuild.changeSets){
        for (entry in changeLogSet.getItems()){
            gitCommitMessage = entry.msg
        }
    }
    return gitCommitMessage
}

#! 在jenkins安装插件 Qy Wechat Notification